package com.sanjiayi.controller;

import com.sanjiayi.dto.MomentPublishDTO;
import com.sanjiayi.dto.MomentDetailDTO;
import com.sanjiayi.pojo.Moment;
import com.sanjiayi.pojo.MomentStats;
import com.sanjiayi.result.PageResult;
import com.sanjiayi.result.QueryPageBean;
import com.sanjiayi.result.Result;
import com.sanjiayi.service.MomentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/moment")
@Api(tags = "动态相关接口")
public class MomentController {

    @Autowired
    private MomentService momentService;

    @PostMapping("/publish")
    @ApiOperation("发布动态")
    public Result publishMoment(
            @ApiParam("用户ID") @RequestParam Integer userId,
            @ApiParam("动态内容") @Validated @RequestBody MomentPublishDTO momentPublishDTO) {
        log.info("接收到发布动态请求，用户ID：{}，内容：{}", userId, momentPublishDTO);
        try {
            momentService.publishMoment(userId, momentPublishDTO);
            log.info("动态发布成功");
            return Result.ok("发布成功");
        } catch (IllegalArgumentException e) {
            log.error("发布动态参数错误：{}", e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("发布动态失败", e);
            return Result.error("发布失败");
        }
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("获取用户动态列表")
    public Result getUserMoments(
            @ApiParam("用户ID") @PathVariable Integer userId,
            @ApiParam("分页参数") QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = momentService.getUserMoments(userId, queryPageBean);
            return Result.ok(pageResult);
        } catch (Exception e) {
            return Result.error("获取动态列表失败");
        }
    }

    @GetMapping("/user/{userId}/recent")
    @ApiOperation("获取用户最近的动态")
    public Result getRecentMoments(
            @ApiParam("用户ID") @PathVariable Integer userId,
            @ApiParam("限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        try {
            List<Moment> moments = momentService.getRecentMoments(userId, limit);
            return Result.ok(moments);
        } catch (Exception e) {
            return Result.error("获取最近动态失败");
        }
    }

    @GetMapping("/topic/{topic}")
    @ApiOperation("根据话题获取动态")
    public Result getMomentsByTopic(
            @ApiParam("话题") @PathVariable String topic,
            @ApiParam("分页参数") QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = momentService.getMomentsByTopic(topic, queryPageBean);
            return Result.ok(pageResult);
        } catch (Exception e) {
            return Result.error("获取话题动态失败");
        }
    }

    @GetMapping("/user/{userId}/stats")
    @ApiOperation("获取用户动态统计信息")
    public Result getUserMomentStats(@ApiParam("用户ID") @PathVariable Integer userId) {
        try {
            MomentStats stats = momentService.getUserMomentStats(userId);
            return Result.ok(stats);
        } catch (Exception e) {
            return Result.error("获取统计信息失败");
        }
    }

    @GetMapping("/recommend")
    @ApiOperation("获取推荐动态列表")
    public Result getRecommendMoments(
            @ApiParam("分页参数") QueryPageBean queryPageBean,
            @ApiParam("推荐类型：latest-最新，hot-热门") @RequestParam(defaultValue = "latest") String type) {
        try {
            PageResult pageResult = momentService.getRecommendMoments(queryPageBean, type);
            return Result.ok(pageResult);
        } catch (Exception e) {
            return Result.error("获取推荐动态失败");
        }
    }

    @GetMapping("/{id}")
    public Result getMomentDetail(@PathVariable("id") String id, @RequestParam(required = false) String userId) {
        try {
            Integer momentId = Integer.parseInt(id);
            Integer userIdInt = userId != null && !userId.equals("undefined") ? Integer.parseInt(userId) : null;
            MomentDetailDTO moment = momentService.getMomentDetail(momentId, userIdInt);
            if (moment == null) {
                return Result.error("文章不存在");
            }
            return Result.ok(moment);
        } catch (NumberFormatException e) {
            return Result.error("参数格式错误");
        }
    }
} 